﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok
{
	[TestFixture]
	public class Test_TaskExtensions
	{
		#region IgnoreExceptions

		[Test]
		public void Test_Task_IgnoreExceptions()
		{
			var task = Task.Factory.StartNew(() =>
				{
					throw new ApplicationException();
				})
				.IgnoreExceptions();

			task = null;

			Thread.Sleep(500);
			GC.Collect(2);
			GC.WaitForPendingFinalizers();
			Thread.Sleep(500);
		}

		[Test]
		public void Test_Task_IgnoreExceptions_Canceling()
		{
			var cancellationTokenSource = new CancellationTokenSource();

			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(500);
				},
				cancellationTokenSource.Token)
				.IgnoreExceptions();

			cancellationTokenSource.Cancel();

			task = null;

			Thread.Sleep(500);
			GC.Collect(2);
			GC.WaitForPendingFinalizers();
			Thread.Sleep(500);
		}

		[Test]
		public void Test_Task_IgnoreExceptions_Null()
		{
			Task task = null;
			Assert.That(task.IgnoreExceptions(), Is.Null);
		}

		[Test]
		public void Test_TaskOfTResult_IgnoreExceptions()
		{
			var task = Task.Factory.StartNew<Int32>(() =>
				{
					throw new ApplicationException();
				})
				.IgnoreExceptions();

			task = null;

			Thread.Sleep(500);
			GC.Collect(2);
			GC.WaitForPendingFinalizers();
			Thread.Sleep(500);
		}

		[Test]
		public void Test_TaskOfTResult_IgnoreExceptions_Canceling()
		{
			var cancellationSource = new CancellationTokenSource();

			var task = Task.Factory.StartNew<Int32>(() =>
				{
					Thread.Sleep(500);
					return 0;
				},
				cancellationSource.Token)
				.IgnoreExceptions();

			cancellationSource.Cancel();

			task = null;

			Thread.Sleep(500);
			GC.Collect(2);
			GC.WaitForPendingFinalizers();
			Thread.Sleep(500);
		}

		[Test]
		public void Test_TaskOfTResult_IgnoreExceptions_Null()
		{
			Task<Int32> task = null;
			Assert.That(task.IgnoreExceptions(), Is.Null);
		}

		#endregion

		#region WaitQuietly

		[Test]
		public void Test_WaitQuietly_Success()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(10);
				});

			task.WaitQuietly();

			Assert.That(task.IsCompleted, Is.True);
			Assert.That(task.IsCanceled, Is.False);
			Assert.That(task.IsFaulted, Is.False);
		}

		[Test]
		public void Test_WaitQuietly_Failure()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(10);
					throw new ApplicationException();
				});

			task.WaitQuietly();

			Assert.That(task.IsCompleted, Is.True);
			Assert.That(task.IsCanceled, Is.False);
			Assert.That(task.IsFaulted, Is.True);
		}

		[Test]
		public void Test_WaitQuietly_Cancel()
		{
			var source = new CancellationTokenSource();
			source.Cancel();

			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(10);
				},
				source.Token);

			task.WaitQuietly();

			Assert.That(task.IsCompleted, Is.True);
			Assert.That(task.IsCanceled, Is.True);
			Assert.That(task.IsFaulted, Is.False);
		}

		[Test]
		public void Test_WaitQuietly_NullTask()
		{
			var task = (Task)null;

			Assert.Throws<ArgumentNullException>(() =>
				{
					task.WaitQuietly();
				});
		}


		[Test]
		public void Test_WaitQuietly_MillisecondsTimeout_Success()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(10);
				});

			var result = task.WaitQuietly(500);

			Assert.That(result, Is.True);
			Assert.That(task.IsCompleted, Is.True);
			Assert.That(task.IsCanceled, Is.False);
			Assert.That(task.IsFaulted, Is.False);
		}

		[Test]
		public void Test_WaitQuietly_MillisecondsTimeout_Timeout()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(500);
				});

			var result = task.WaitQuietly(10);

			Assert.That(result, Is.False);
			Assert.That(task.IsCompleted, Is.False);
			Assert.That(task.IsCanceled, Is.False);
			Assert.That(task.IsFaulted, Is.False);
		}

		[Test]
		public void Test_WaitQuietly_MillisecondsTimeout_NullTask()
		{
			var task = (Task)null;

			Assert.Throws<ArgumentNullException>(() =>
				{
					task.WaitQuietly(500);
				});
		}

		[Test]
		public void Test_WaitQuietly_MillisecondsTimeout_NegativeTimeout()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(10);
				});

			Assert.Throws<ArgumentOutOfRangeException>(() =>
				{
					task.WaitQuietly(-25);
				});
		}


		[Test]
		public void Test_WaitQuietly_Timeout_Success()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(10);
				});

			var result = task.WaitQuietly(TimeSpan.FromMilliseconds(500));

			Assert.That(result, Is.True);
			Assert.That(task.IsCompleted, Is.True);
			Assert.That(task.IsCanceled, Is.False);
			Assert.That(task.IsFaulted, Is.False);
		}

		[Test]
		public void Test_WaitQuietly_Timeout_Timeout()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(500);
				});

			var result = task.WaitQuietly(TimeSpan.FromMilliseconds(10));

			Assert.That(result, Is.False);
			Assert.That(task.IsCompleted, Is.False);
			Assert.That(task.IsCanceled, Is.False);
			Assert.That(task.IsFaulted, Is.False);
		}

		[Test]
		public void Test_WaitQuietly_Timeout_NullTask()
		{
			var task = (Task)null;

			Assert.Throws<ArgumentNullException>(() =>
				{
					task.WaitQuietly(TimeSpan.FromMilliseconds(500));
				});
		}

		[Test]
		public void Test_WaitQuietly_Timeout_NegativeTimeout()
		{
			var task = Task.Factory.StartNew(() =>
				{
					Thread.Sleep(10);
				});

			Assert.Throws<ArgumentOutOfRangeException>(() =>
				{
					task.WaitQuietly(TimeSpan.FromMilliseconds(-25));
				});
		}

		#endregion

	}
}

#endif
